package samples.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class SampleDatabaseWebservice
{	

	public String getName(int id)
		throws Exception
	{
		String result = "";
		
		try {
			// initialize the database connection
			SampleDatabaseManager.init();
			// get a connection
			Connection conn = SampleDatabaseManager.getConnection();
			
			// prepare a SELECT ...
			PreparedStatement stmt = conn.prepareStatement("SELECT * FROM myTable WHERE id=?");
		    stmt.setInt(1, id);
	   	    
	   	    // ... and execute it
	   	    ResultSet rs = stmt.executeQuery();
	   	    	   	    
	   	   	// check for a result
		    if(rs.next()) {
		    	result = rs.getString("name");
		    } else {
		   	    SampleDatabaseManager.freeConnection(conn);
		    	throw new Exception("Cannot find id '" + id + "' ...");
			}
			
			// put the connection back into the pool
	   	    SampleDatabaseManager.freeConnection(conn);
		} 
		catch(Exception e) {
			throw new Exception("Database problem: " + e.getMessage());		    	
		}
		
		return result;
	}	

}